
#include "cumsum_tiling.h"
#include "register/op_def_registry.h"
#include "tiling_common.h"
#include <cstdint>

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext *context) {

  CumsumTilingData tiling;
  context->SetBlockDim(1);
  auto x_shape = context->GetInputShape(0)->GetStorageShape();

  int64_t xshape[64] = {0};
  xshape[0] = x_shape.GetDimNum();
  shape_for(x_shape,
            [&](int64_t i, int64_t shapei) { xshape[i + 2] = shapei; });
  tiling.set_xShape(xshape);

  auto exclusive = *(context->GetAttrs()->GetBool(0));
  tiling.set_exclusive(exclusive);
  auto reverse = *(context->GetAttrs()->GetBool(1));
  tiling.set_reverse(reverse);

  // if (!exclusive && !reverse) {
  //   context->SetTilingKey(0);
  // } else if (!exclusive && reverse) {
  //   context->SetTilingKey(1);
  // } else if (exclusive && !reverse) {
  //   context->SetTilingKey(2);
  // } else {
  //   context->SetTilingKey(3);
  // }

  COMMON_TILING_GET_PLATFORM;
  COMMON_TILING_GET_UB_SIZE;
  tiling.set_ubSize(ub_size);

  tiling_zero_workspace(context);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
                      context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

  print_shape_vector("xshape = ", xshape);
  std::cout << "exclusive = " << exclusive << std::endl;
  std::cout << "reverse = " << reverse << std::endl;
  std::cout << "ub_size = " << tiling.get_ubSize() << std::endl;

  return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext *context) {
  const gert::Shape *x1_shape = context->GetInputShape(0);
  gert::Shape *y_shape = context->GetOutputShape(0);
  *y_shape = *x1_shape;
  return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops {
class Cumsum : public OpDef {
public:
  explicit Cumsum(const char *name) : OpDef(name) {
    this->Input("x")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat(
            {ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
    this->Input("axis")
        .ParamType(REQUIRED)
        .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat(
            {ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
    this->Output("y")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat(
            {ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
    this->Attr("exclusive").AttrType(OPTIONAL).Bool(false);
    this->Attr("reverse").AttrType(OPTIONAL).Bool(false);

    this->SetInferShape(ge::InferShape);

    this->AICore().SetTiling(optiling::TilingFunc);
    this->AICore().AddConfig("ascend310b");
  }
};

OP_ADD(Cumsum);
} // namespace ops
